package com.alibaba.dubbo.common.utils;

import java.io.File;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class MimeTypeUtils {

	private MimeTypeUtils() {
	}

	@SuppressWarnings("serial")
	private final static Map<String, String> MIME_MapTable = new HashMap<String, String>() {
		{
			// {后缀名，MIME类型}
			put("3gp", "video/3gpp");
			put("apk", "application/vnd.android.package-archive");
			put("asf", "video/x-ms-asf");
			put("avi", "video/x-msvideo");
			put("bin", "application/octet-stream");
			put("bmp", "image/bmp");
			put("c", "text/plain");
			put("class", "application/octet-stream");
			put("conf", "text/plain");
			put("cpp", "text/plain");
			put("doc", "application/msword");
			put("docx",
					"application/vnd.openxmlformats-officedocument.wordprocessingml.document");
			put("xls", "application/vnd.ms-excel");
			put("xlsx",
					"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
			put("exe", "application/octet-stream");
			put("gtar", "application/x-gtar");
			put("gz", "application/x-gzip");
			put("h", "text/plain");
			put("htm", "text/html");
			put("html", "text/html");
			put("jar", "application/java-archive");
			put("java", "text/plain");
			put("gif", "image/gif");
			put("jpeg", "image/jpeg");
			put("jpg", "image/jpeg");
			put("png", "image/png");
			put("bmp", "image/bmp");
			put("js", "application/x-javascript");
			put("log", "text/plain");
			put("m3u", "audio/x-mpegurl");
			put("m4a", "audio/mp4a-latm");
			put("m4b", "audio/mp4a-latm");
			put("m4p", "audio/mp4a-latm");
			put("m4u", "video/vnd.mpegurl");
			put("m4v", "video/x-m4v");
			put("mov", "video/quicktime");
			put("mp2", "audio/x-mpeg");
			put("mp3", "audio/x-mpeg");
			put("mp4", "video/mp4");
			put("mpc", "application/vnd.mpohun.certificate");
			put("mpe", "video/mpeg");
			put("mpeg", "video/mpeg");
			put("mpg", "video/mpeg");
			put("mpg4", "video/mp4");
			put("mpga", "audio/mpeg");
			put("msg", "application/vnd.ms-outlook");
			put("ogg", "audio/ogg");
			put("pdf", "application/pdf");
			put("pps", "application/vnd.ms-powerpoint");
			put("ppt", "application/vnd.ms-powerpoint");
			put("pptx",
					"application/vnd.openxmlformats-officedocument.presentationml.presentation");
			put("prop", "text/plain");
			put("rc", "text/plain");
			put("rmvb", "audio/x-pn-realaudio");
			put("rtf", "application/rtf");
			put("sh", "text/plain");
			put("tar", "application/x-tar");
			put("tgz", "application/x-compressed");
			put("txt", "text/plain");
			put("wav", "audio/x-wav");
			put("wma", "audio/x-ms-wma");
			put("wmv", "audio/x-ms-wmv");
			put("wps", "application/vnd.ms-works");
			put("xml", "text/plain");
			put("z", "application/x-compress");
			put("zip", "application/x-zip-compressed");
			put("", "*/*");
		}
	};

	public static String getMimeTypeByExt(final String ext) {
		String type = "application/octet-stream";
		// 获取后缀名前的分隔符"."在fName中的位置。
		if (ext == null || ext.length() == 0)
			return type;
		// 在MIME和文件类型的匹配表中找到对应的MIME类型。
		type = MIME_MapTable.get(ext);
		if (type == null)
			type = "application/octet-stream";
		return type;
	}

	public static String getMimeType(final String fileName) {
		String type = "application/octet-stream";
		// 获取后缀名前的分隔符"."在fName中的位置。
		int dotIndex = fileName.lastIndexOf(".");
		if (dotIndex < 0) {
			return type;
		}
		/* 获取文件的后缀名 */
		String ext = fileName.substring(dotIndex + 1).toLowerCase(
				Locale.getDefault());
		if (ext == "")
			return type;
		// 在MIME和文件类型的匹配表中找到对应的MIME类型。
		type = MIME_MapTable.get(ext);
		if (type == null)
			type = "application/octet-stream";
		return type;
	}

	static public String getMimeType(File file) {
		String fileName = file.getName();
		return getMimeType(fileName);
	}

	static public String getExtension(final String fileName) {
		// 获取后缀名前的分隔符"."在fName中的位置。
		int dotIndex = fileName.lastIndexOf(".");
		if (dotIndex < 0) {
			return null;
		}
		/* 获取文件的后缀名 */
		String ext = fileName.substring(dotIndex + 1).toLowerCase(
				Locale.getDefault());
		return ext;
	}
}
